package com.cardone.platform.configuration.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.configuration.dto.DictionaryDto;
import com.cardone.platform.configuration.service.DictionaryService;

/**
 * 字典
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/configuration/dictionary/")
@Controller
public class DictionaryController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 字典：删除
	 *
	 * @param deleteDictionary
	 *          字典对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final DictionaryDto deleteDictionary) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(DictionaryService.class).deleteByIds(deleteDictionary);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryController.log);
				}
			}
		};
	}

	/**
	 * 字典：分页查询
	 *
	 * @param findDictionary
	 *          字典对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final DictionaryDto findDictionary, final Model model) {
		final DictionaryDto dictionary = ContextHolder.getBean(DictionaryService.class).findById(DictionaryDto.class, findDictionary);

		model.addAttribute("dictionary", dictionary);
	}

	/**
	 * 字典：编辑
	 *
	 * @param findDictionary
	 *          字典对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<DictionaryDto>> findByIdJson(final DictionaryDto findDictionary, final Model model) {
		return new Callable<ReturnDataDto<DictionaryDto>>() {
			@Override
			public ReturnDataDto<DictionaryDto> call() throws Exception {
				try {
					final DictionaryDto dictionary = ContextHolder.getBean(DictionaryService.class).findById(DictionaryDto.class, findDictionary);

					return ReturnDataUtils.newReturnData(dictionary);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryController.log);
				}
			}
		};
	}

	/**
	 * 字典：查询
	 *
	 * @param findListDictionary
	 *          字典对象
	 * @return 字典对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<DictionaryDto>>> findListByLikeCodeJson(final DictionaryDto findListDictionary) {
		return new Callable<ReturnDataDto<List<DictionaryDto>>>() {
			@Override
			public ReturnDataDto<List<DictionaryDto>> call() throws Exception {
				try {
					final List<DictionaryDto> dictionaryList = ContextHolder.getBean(DictionaryService.class).findListByLikeCode(DictionaryDto.class, findListDictionary);

					return ReturnDataUtils.newReturnData(dictionaryList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryController.log);
				}
			}
		};
	}

	/**
	 * 字典：分页查询
	 *
	 * @param paginationDictionary
	 *          字典对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<DictionaryDto>>> paginationByLikeCodeJson(final DictionaryDto paginationDictionary) {
		return new Callable<ReturnDataDto<PaginationDto<DictionaryDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<DictionaryDto>> call() throws Exception {
				try {
					final PaginationDto<DictionaryDto> dictionaryPagination = ContextHolder.getBean(DictionaryService.class).paginationByLikeCode(DictionaryDto.class, paginationDictionary);

					return ReturnDataUtils.newReturnData(dictionaryPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryController.log);
				}
			}
		};
	}

	/**
	 * 字典：分页查询
	 *
	 * @param paginationDictionary
	 *          字典对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final DictionaryDto paginationDictionary, final Model model) {
		final PaginationDto<DictionaryDto> dictionaryPagination = ContextHolder.getBean(DictionaryService.class).paginationByLikeCode(DictionaryDto.class, paginationDictionary);

		model.addAttribute("dictionaryPagination", dictionaryPagination);
	}

	/**
	 * 字典：保存
	 *
	 * @param saveDictionary
	 *          字典对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final DictionaryDto saveDictionary) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(DictionaryService.class).saveByIdOrCode(DictionaryDto.class, saveDictionary);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryController.log);
				}
			}
		};
	}
}
